package unitTest;

import com.mic.yzzn.plc.common.control.RMI.PLCCollector;
import com.mic.yzzn.plc.common.control.datastructure.PLCMission;
import com.mic.yzzn.plc.common.ins.datastructure.PLCDataRegisterNumber;
import com.mic.yzzn.plc.common.instructions.basic.PLCRequestInstruction;
import com.mic.yzzn.plc.common.instructions.request.PLCDataRegisterReadInstruction;
import com.mic.yzzn.plc.common.toolUtils.PLCDataCode;

import java.rmi.Naming;
import java.util.ArrayList;



/**
 * Created by ASH on 16/4/27.
 */
public class DPointReadClientTest
{
    public static void main(String args[])
    {
        String name = "125.216.244.253-9094-01"; // 要发送任务的采集器的ID
        int interval = 1; // 发送任务的间隔, 单位为s
        String beginAddress = "20000"; // 指令采集点位的起始地址, 将采集该地址后的30 * addressInterval个点
        int addressInterval = 1; // 每条指令采集地址的间隔, 最大为20, 最小为1

        int begin = Integer.parseInt(beginAddress);
        ArrayList<PLCRequestInstruction> instructions = new ArrayList<>();
        for (int i = 0; i < 30; ++i)
        {
            int end = begin + addressInterval - 1;
            PLCDataRegisterNumber dNumber = new PLCDataRegisterNumber(String.valueOf(begin), String.valueOf(end));
            PLCDataRegisterReadInstruction instruction = new PLCDataRegisterReadInstruction();
            try
            {
                instruction.setLocationNumber("01");
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            instruction.setDataCode(PLCDataCode.PLC_DATA_REGISTER_CODE);
            instruction.setDataRegisterNumber(dNumber);
            instruction.setBBC();

            instructions.add(instruction);
            begin = end + 1;
        }

        PLCMission mission = new PLCMission(
                PLCMission.PLCMissionTypeReadNormal, // 设置任务的类型
                PLCMission.PLCMISSION_PRIORITY_RTR_1 // 设置任务的优先级队列
        );
        mission.setInstructions(instructions);

        PLCCollector collector = null;

        try
        {
            collector = (PLCCollector) Naming.lookup(name);

            while (true)
            {
                System.out.println(collector.addRealtimeReadMission(mission));

                Thread.sleep(interval * 1000);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
